﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SQLite;
using MedicLibrary.Controler;

namespace MedicLibrary.Model
{


    public class HoatChatThuoc
    {
        public int ID { get; set; }
        public int HoatChatID { get; set; }
        public int ThuocID { get; set; }
        public float HamLuong { get; set; }
        public String DonVi { get; set; }
        public String GhiChu { get; set; }

        public HoatChatThuoc()
        {
       
        }

        public static DataTable getDanhSachHoatChat(int IDThuoc)
        {
            DBConnector dbc = new DBConnector();
            String sql = "";
            if (IDThuoc >= 0)
            {
                sql = "SELECT HoatChatThuoc.ID, HoatChat.TenHoatChat, HoatChatThuoc.HamLuong, HoatChatThuoc.DonVi FROM HoatChatThuoc, HoatChat WHERE ThuocID == " + IDThuoc + " AND HoatChatThuoc.HoatChatID = HoatChat.ID LIMIT 20";
            }
            else
                MessageBox.Show("ID Thuốc không hợp lệ!");
            return dbc.GetDataTable(sql);
        }

        public static DataTable getThongTinHoatChat(int ID)
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT * FROM HoatChat ";
            sql += "WHERE ID =" + ID;
            return dbc.GetDataTable(sql);

        }

        public bool insertHoatChat()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("ID", this.ID.ToString());
            data.Add("HoatChatID", this.HoatChatID.ToString());
            data.Add("ThuocID", this.ThuocID.ToString());
            data.Add("HamLuong", this.HamLuong.ToString());
            data.Add("DonVi", this.DonVi);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Insert("HoatChatThuoc", data);
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            return true;

        }


        /*public bool readThongTinHoatChat(int ID)
        {
            DataTable table = getThongTinHoatChat(ID);
            if (table.Rows.Count == 0)
                return false;
            foreach (DataRow r in table.Rows)
            {
                this.ID = Int32.Parse(r["ID"].ToString());
                this.TenHoatChat = r["TenHoatChat"].ToString();
                this.MaHoatChat = r["MaHoatChat"].ToString();
                this.DacDiem = r["DacDiem"].ToString();
                this.GhiChu = r["GhiChu"].ToString();
            }
            return true;
        }
        
        public bool insertHoatChat()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("ID", this.ID.ToString());
            data.Add("MaHoatChat", "HCH" + Session.generateID(4, this.ID));
            data.Add("TenHoatChat", this.TenHoatChat);
            data.Add("DacDiem", this.DacDiem);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Insert("HoatChat", data);
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Thêm Hoạt Chất thành công!");
            return true;

        }

        public bool updateHoatChat()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("MaHoatChat", this.MaHoatChat);
            data.Add("TenHoatChat", this.TenHoatChat);
            data.Add("DacDiem", this.DacDiem);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Update("HoatChat", data, String.Format("HoatChat.ID = {0}", this.ID));
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Cập nhật Hoạt Chất thành công!");
            return true;
        }
*/
        public bool deleteHoatChat()
        {
            
            if (MessageBox.Show("Bạn thật sự muốn xóa Hoạt Chất này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                DBConnector dbc = new DBConnector();
                return dbc.Delete("HoatChatThuoc", String.Format("ID = {0}", this.ID));
            }
            else
                return false;
        }

        public void getMaxID()
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT MAX(ID) as MAXID FROM HoatChatThuoc";
            this.ID = Int32.Parse(dbc.GetDataTable(sql).Rows[0]["MAXID"].ToString()) + 1;
        }

     }
}
